import React from 'react';
import { debounce } from 'lodash';
import { getSalaryListByPositionValue } from '@/services/organization';

const useSalaryList = query => {
    const [data, setData] = React.useState([]);
    const [loading, setLoading] = React.useState(false);

    const fetchData = async positionValue => {
        setLoading(true);
        const { data: dataList, code } = (await getSalaryListByPositionValue(positionValue)) || {};

        if (code === 10000) {
            setData(dataList);
        }

        setLoading(false);
    };

    const debounceFn = React.useRef(debounce(fetchData, 500));

    React.useEffect(() => {
        if (query.positionValue) {
            debounceFn.current(query.positionValue);
        }
    }, [query.positionValue]);

    return [loading, data];
};

export default useSalaryList;
